package com.molin.setTest;

import java.util.Comparator;
import java.util.TreeSet;

public class MyTreeSet3 {
    public static void main(String[] args) {
        TreeSet<Teacher> teachers=new TreeSet<>(new Comparator<Teacher>() {
            @Override
            public int compare(Teacher o1, Teacher o2) {
                //o1,表示当前存入的对象，o2表示之前存入的对象。
                //主要条件
                int result=o1.getAge()-o2.getAge();
                //次要条件
                result=result==0?o1.getName().compareTo(o2.getName()):result;
                return result;
            }
        });
        teachers.add(new Teacher("zhangsan",23));
        teachers.add(new Teacher("lisi",23));
        teachers.add(new Teacher("lisi",23));
        teachers.add(new Teacher("wangwu",27));
        teachers.add(new Teacher("mazi",20));
        System.out.println(teachers);
    }
}
class Teacher{
    private String name;
    private int age;

    public Teacher(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Teacher{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}